package com.echat.messagesender.sender.email.impl;

import com.echat.commonlib.util.StringUtil;
import com.echat.messagesender.sender.email.EmailSender;
import com.echat.messagesender.sender.email.bean.EmailConfig;
import com.sendcloud.sdk.core.SendCloud;
import com.sendcloud.sdk.model.MailAddressReceiver;
import com.sendcloud.sdk.model.MailBody;
import com.sendcloud.sdk.model.SendCloudMail;
import com.sendcloud.sdk.model.TextContent;
import com.sendcloud.sdk.util.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by huangzhilin on 2018/5/10.
 */
public class SendCloudEmailSender extends EmailSender {
    private static final Logger logger = LoggerFactory.getLogger(SendCloudEmailSender.class);
    private SendCloud sc;
    private EmailConfig config;

    public SendCloudEmailSender(EmailConfig config) {
        SendCloud sendCloud = new SendCloud();
        sendCloud.setMailAPI(config.getSendApi());
        sendCloud.setSmsAPI(config.getSendSmsApi());
        sendCloud.setTemplateAPI(config.getSendTemplateApi());
        sendCloud.setVoiceAPI(config.getSendVoiceApi());
        sendCloud.setApiKey(config.getApiKey());
        sendCloud.setApiUser(config.getApiUser());
        this.sc = sendCloud;
        this.config = config;
        super.platformAccountId = config.getPlatformAccountId();
        super.platformName = config.getPlatformName();
    }

    /**
     * @throws Exception
     */
    public SendCloudEmailSender(SendCloud sc) {
        this.sc = sc;
    }

    @Override
    public boolean sendEmail(String[] receives, String subject, String htmlBody, String[] ccReceives, String[] bccReceiver, String replayEmail) {
        try {
            SendCloudMail mail = getMail(receives, subject, htmlBody , replayEmail);
            ResponseData res = sc.sendMail(mail);
            if (res != null && StringUtil.equals("200", res.getStatusCode())) {
                return true;
            } else {
                return false;
            }
        } catch (Throwable e) {
            logger.error("SendCloudEmailSender.sendHtmlEmailWithAttachment.error.", e);
            return false;
        }
    }

    private SendCloudMail getMail(String[] receives, String subject, String body, String replayEmail) throws Exception {
        MailAddressReceiver receiver = new MailAddressReceiver();
        // 添加收件人
        receiver.addTo(receives);
        MailBody mailBody = new MailBody();
        // 设置 From
        mailBody.setFrom(config.getEmail());
        // 设置 FromName
        mailBody.setFromName(config.getEmailName());
        mailBody.setSubject(subject);
        //设置回复
        mailBody.setReplyTo(replayEmail);

        TextContent content = new TextContent();
        content.setContent_type(TextContent.ScContentType.html);
        content.setText(body);

        SendCloudMail mail = new SendCloudMail();
        mail.setTo(receiver);
        mail.setBody(mailBody);
        mail.setContent(content);
        return mail;
    }
}
